package com.search;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
 
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
 
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
 
/**
 * The Class JDBCDatabaseUtil.
 * @author prabhat.jha
 */
public class JDBCDatabaseUtil {
    /**
     * Gets the data source.
     * 
     * @return the data source
     */
	//@Value("${jdbc.searchdatabaseurl}")
	private static String jdbc_url = "jdbc:mysql://localhost:3306/askme_new?emulateLocators=true&useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false";
	//@Value("${jdbc.username}")
	private static String username = "root";
	//@Value("${jdbc.password}")
	private static String password = "123456";
	
    public static DataSource getDataSource() {
//    	Resource resource = new ClassPathResource("/application.properties");
//    	try {
//			Properties props = PropertiesLoaderUtils.loadProperties(resource);
//			props.get("");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    	
    	System.out.println(JDBCDatabaseUtil.jdbc_url);
    	
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser(JDBCDatabaseUtil.username);
        dataSource.setPassword(JDBCDatabaseUtil.password);
        dataSource.setUrl(JDBCDatabaseUtil.jdbc_url);
        return dataSource;
    }
 
    /**
     * Gets the connection.
     * 
     * @return the connection
     * @throws SQLException
     *             the sQL exception
     */
    public static Connection getConnection() throws SQLException {
        return getDataSource().getConnection();
    }
}